<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 客户信息管理
 *
 * @icon fa fa-circle-o
 */
class ClothmakingCustomer extends Backend
{

    /**
     * ClothmakingCustomer模型对象
     * @var \app\admin\model\ClothmakingCustomer
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\ClothmakingCustomer;
        $this->view->assign("paymentList", $this->model->getPaymentList());
    }

    /**
     * 获取客户列表（返回 id 和 customer_name）
     * 
     * @return \think\Response
     */
    public function getCustomerInfo()
    {
        // 设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        // 查询条件（可根据需求调整）
        $where = [];
        $search = $this->request->request("search");
        if ($search) {
            $where[] = ['customer_name', 'like', "%{$search}%"];
        }

        // 查询数据（返回 id 和 customer_name 字段）
        $list = $this->model
            ->where($where)
            ->field('id, customer_name')
            ->order('id', 'desc')
            ->select();

        // 返回 JSON 数据
        $result = [
            "total" => count($list),
            "rows" => $list,
        ];

        return json($result);
    }

    /**
     * 重写selectpage方法，使用customer_name字段
     */
    public function selectpage()
    {
        $this->dataLimit = 'auth';
        $this->dataLimitField = 'user_ids';

        $search = $this->request->get("search", '');
        $page = $this->request->get("pageNumber", 1);
        $pageSize = $this->request->get("pageSize", 10);
        $sort = $this->request->get("sort", 'id');
        $order = $this->request->get("order", 'desc');
        $filter = $this->request->get("filter", '');
        $op = $this->request->get("op", '', 'trim');

        $filter = (array) json_decode($filter, true);
        $op = (array) json_decode($op, true);
        $filter = $filter ? $filter : [];
        $op = $op ? $op : [];

        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }

        if ($search !== '') {
            $this->model->where('customer_name', 'like', "%{$search}%");
        }

        $list = $this->model
            ->where($filter)
            ->order($sort, $order)
            ->page($page, $pageSize)
            ->field('id, customer_name')
            ->select();

        $total = $this->model
            ->where($filter)
            ->count();

        $result = ['total' => $total, 'rows' => $list];
        return json($result);
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
